package com.example.coffeeseller_2.model.helper;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.example.coffeeseller_2.model.beans.MachineConfig;
import com.example.coffeeseller_2.model.contentprovider.Constance;
import com.example.coffeeseller_2.model.contentprovider.SharedPreferencesManager;
import com.example.coffeeseller_2.model.httputils.JsonUtil;
import com.example.coffeeseller_2.model.httputils.OkHttpUtil;
import com.example.coffeeseller_2.utils.SecondToDate;

import org.json.JSONException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by WH on 2018/9/6.
 * 提交清洗记录
 */

public class CommitClearRecord {
    private static String TAG = "CommitClearRecord";
    private static CommitClearRecord mInstance;

    public static CommitClearRecord getInstance() {
        if (mInstance == null) {
            mInstance = new CommitClearRecord();
        }
        return mInstance;
    }

    public void CleaningRecordsCommit(final Context context) {
        final Map<String, Object> postBody = new HashMap<>();
        postBody.put("machineCode", MachineConfig.getMachineCode());
        postBody.put("loginPassword", SharedPreferencesManager.getInstance(context).getLoginPassword());
        long cleaningTime = SharedPreferencesManager.getInstance(context).getLastCleanningTime();
        postBody.put("cleaningTime", SecondToDate.getDateToString(cleaningTime));

        Log.d(TAG, " request params= " + postBody.toString());

        new AsyncTask<Void, Boolean, Boolean>() {
            String response;

            @Override
            protected Boolean doInBackground(Void... params) {
                try {
                    org.json.JSONObject object = new org.json.JSONObject(OkHttpUtil.post(Constance.COMMIT_CLEANNINGRECORD_URL, JsonUtil.mapToJson(postBody)));
                    if (object == null) {
                        Log.e(TAG, "object is null");
                        return false;
                    }
                    Log.e(TAG, "result is " + object.toString());
                    if (object.getString("err").equals("")) {
                        response = "";
                        return true;
                    } else {
                        response = object.getString("err");
                    }
                } catch (IOException e) {
                    response = e.getMessage();
                } catch (JSONException e) {
                    response = e.getMessage();
                }
                return false;
            }

            @Override
            protected void onPostExecute(Boolean aBoolean) {
                if (aBoolean) {
                    Log.d(TAG, "清洗记录提交成功");
//                    Toast.makeText(context, "", Toast.LENGTH_SHORT).show();
                } else {
//                    Toast.makeText(context, response + "\t提交失败", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, response + "\t提交失败");
                }
            }
        }.execute();
    }
}
